package datastore.api.impl.serializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class JavaSerializer implements Serializer {

    @Override
    public Object fromBytes(byte[] bytes) throws Exception {
        ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(bytes));
        Object o = oin.readObject();
        oin.close();
        return o;
    }

    @Override
    public byte[] toBytes(Object o) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oout = new ObjectOutputStream(baos);
        oout.writeObject(o);
        oout.flush();
        oout.close();
        byte[] bytes = baos.toByteArray();
        return bytes;
    }

}
